package com.xique.system.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.StringUtils;
import com.xique.system.bean.domain.ParkInfo;
import com.xique.system.bean.search.ParkInfoSearch;
import com.xique.system.mapper.ParkInfoMapper;
import com.xique.system.service.service.IParkInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shd
 * @create 2021/11/12 13:35
 */
@Service
public class ParkInfoServiceImpl extends ServiceImpl<ParkInfoMapper, ParkInfo> implements IParkInfoService {

    @Autowired
    private ParkInfoMapper parkInfoMapper;

    @Override
    public ParkInfo selectParkInfoById(Long id) {
        if (StringUtils.isNull(id)) {
            return null;
        }
        return parkInfoMapper.selectById(id);
    }

    @Override
    public ParkInfo selectParkInfoBySearch(ParkInfoSearch search) {

        QueryWrapper<ParkInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(search.getCode())) {
            wrapper.eq("code", search.getCode());
        }
        if (StringUtils.isNotNull(search.getParkId())) {
            wrapper.eq("id", search.getParkId());
        }
        if (StringUtils.isNotEmpty(search.getName())) {
            wrapper.eq("name", search.getName());
        }
        if (StringUtils.isNotEmpty(search.getStatus())) {
            wrapper.eq("status", search.getStatus());
        }
        if (StringUtils.isNotEmpty(search.getRecordStatus())) {
            wrapper.eq("record_status", search.getRecordStatus());
        }
        wrapper.last("limit 1");

        return parkInfoMapper.selectOne(wrapper);
    }

    @Override
    public List<ParkInfo> selectParkInfoListBySearch(ParkInfoSearch search) {

        QueryWrapper<ParkInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(search.getCode())) {
            wrapper.eq("code", search.getCode());
        }
        if (StringUtils.isNotNull(search.getParkId())) {
            wrapper.eq("id", search.getParkId());
        }
        if (StringUtils.isNotEmpty(search.getName())) {
            wrapper.eq("name", search.getName());
        }
        if (StringUtils.isNotEmpty(search.getStatus())) {
            wrapper.eq("status", search.getStatus());
        }
        if (StringUtils.isNotEmpty(search.getRecordStatus())) {
            wrapper.eq("record_status", search.getRecordStatus());
        }
        if (StringUtils.isNotEmpty(search.getIsDoor())) {
            wrapper.eq("is_door", search.getIsDoor());
        }
        if (StringUtils.isNotEmpty(search.getDistrict())) {
            wrapper.eq("district", search.getDistrict());
        }
        wrapper.eq("record_status", "normal");
        wrapper.orderByDesc("create_time");

        return parkInfoMapper.selectList(wrapper);
    }

    @Override
    public IPage<ParkInfo> selectParkInfoPageBySearch(ParkInfoSearch search) {

        QueryWrapper<ParkInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("record_status", "normal");
        if (StringUtils.isNotEmpty(search.getCode())) {
            wrapper.eq("code", search.getCode());
        }
        if (StringUtils.isNotNull(search.getDeptId())) {
            wrapper.eq("dept_id", search.getDeptId());
        }
        if (StringUtils.isNotEmpty(search.getName())) {
            wrapper.like("name", search.getName());
        }
        if (StringUtils.isNotEmpty(search.getParams())) {
            wrapper.apply(search.getParams().get("dataScope").toString());
        }
        if (StringUtils.isNotEmpty(search.getStatus())) {
            wrapper.eq("status", search.getStatus());
        }
        if (CollectionUtils.isNotEmpty(search.getDeptIds())) {
            wrapper.in("dept_id", search.getDeptIds());
        }

        if (StringUtils.isNotEmpty(search.getIsPark())) {
            wrapper.in("is_park", search.getIsPark());
        }
        if (StringUtils.isNotEmpty(search.getIsDoor())) {
            wrapper.in("is_door", search.getIsDoor());
        }
        if (StringUtils.isNotEmpty(search.getVerson())) {
            wrapper.eq("verson", search.getVerson());
        }
        wrapper.orderByDesc("update_time","id");
        Page<ParkInfo> page = new Page<>();
        if (null != search.getPageNum() && null != search.getPageSize()) {
            page.setCurrent(search.getPageNum());
            page.setSize(search.getPageSize());
        }
        return parkInfoMapper.selectPage(page, wrapper);
    }
}
